<script >
  import * as echarts from "echarts";
  import 'echarts/lib/chart/pie' // 引入饼图图表
  import 'echarts/lib/component/tooltip'
  import axios from "axios";
  export default {
    async created() {
      try {
        axios({
          method:"get",
          url:"http://localhost:5000//admin/chart/pie",
          params:{
            admin_id:"27"
          }
        }).then((res)=>{
          console.log(res.data.data)
          this.pieData=res.data.data
          console.log(this.pieData)
          this.$nextTick(() => {
            this.initDate(); //数据初始化
            this.initEcharts();
          })

        })
      } catch (error) {
        console.error(error);
      }
    },
    methods: {
      jumpBook() {
        this.$router.push('/admin/book')
      },
      jumpUser() {
        this.$router.push('/admin/user')
      },
      jumpAnnouncement() {
        this.$router.push('/admin/announcement')
      },
      jumpChart() {
        this.$router.push('/admin/chart')
      },
      jumpEdit() {
        this.$router.push('/admin/edit')
      },
      jumpChat(){
        this.$router.push('/admin/chat')
      },
      initDate() {
        for (let i = 0; i < this.pieData.length; i++) {
          // this.xData[i] = i;
          // this.yData =this.xData[i]*this.xData[i];
          this.pieName[i] = this.pieData[i].name;
        }
      },
      initEcharts() {
        // 饼图
        const option = {
          legend: {
            // 图例
            data: this.pieName,
            right: "10%",
            top: "85%",
            x:"center",
            y:"bottom"
          },
          title: {
            // 设置饼图标题，位置设为顶部居中
            text: "借阅量分类",
            top: "5%",
            left: "center",
            textStyle:{
              backgroundColor:{

              }
            }
          },
          series: [
            {
              type: "pie",
              label: {
                show: true,
                formatter: "{b} : {c} ({d}%)" // b代表名称，c代表对应值，d代表百分比
              },
              radius: "40%", //饼图半径
              data: this.pieData
            }
          ]
        };
        console.log(this.pieData);
        const optionFree = {
          xAxis: {},
          yAxis: {},
          series: [
            {
              data: this.pieData,
              type: "line",
              smooth: true
            }
          ]
        };
        this.myChart = echarts.init(document.getElementById("mychart"));
        this.myChart.setOption(option);
        //随着屏幕大小调节图表
        window.addEventListener("resize", () => {
          this.myChart.resize();
        });
      }
    },
    // mounted() {
    //   this.initEcharts();
    // },
    mounted() {

    },
    data() {
      return {
        // name: "张雪",
        // xData: ["2020-02", "2020-03", "2020-04", "2020-05"], //横坐标数据
        // yData: [30, 132, 80, 134] //纵坐标数据，与横坐标对应
        myChart: {},
        pieData: [
          {
            value: 463,
            name: "自然类"
          },
          {
            value: 395,
            name: "文学类"
          },
          {
            value: 157,
            name: "技术类"
          },
          {
            value: 149,
            name: "政治类"
          },
          {
            value: 147,
            name: "其他"
          }
        ],
        pieName: [],
        myChartStyle: { float: "left", width: "100%", height: "400px" }
      }
    }
  }
</script>

<template>
  <div >
    <ul>
      <li>
        <img src="../../assets/img/common/logo.png" alt = "logo" class = "logo">
      </li>
      <li class="other">
        <a @click = jumpBook>图书管理</a>
      </li>
      <li  class="other">
        <a @click="jumpUser">用户管理</a>
      </li>
      <li  class="other">
        <a @click=jumpAnnouncement>公告管理</a>
      </li>
      <li class="current">
        <a @click = jumpChart>数据报表</a>
      </li>
      <li class="other">
        <a @click = jumpEdit>个人中心</a>
      </li >
      <li class="other2" v-on:click="jumpChat">
        <img src="../../assets/img/common/msg.jpg" alt="msg" class="logo" >
      </li>
    </ul>
    <img src="../../assets/img/admin/chart.png" alt="数据报表" class="top-img">
    <div class="charts">
      <div class="pic-div">
        <img src="../../assets/img/admin/2.png" alt="chart" class="chart-img">
        <br>
        <img src="../../assets/img/admin/1.png" alt="chart" class="chart-img">
      </div>
      <div class="pie-div">
        <div class="echart" id="mychart" :style="myChartStyle"></div>
      </div>


    </div>
  </div>
</template>

<style scoped>
ul{
  display: flex;
  background-color: rgb(168,189,203);
  margin: 0;
}
li{
  display: inline-block;
  height: 80%;
  width: 12%;
  padding: 1% 0;
  margin: auto ;
  text-align: center;
}
li.current{
  border-bottom: 5px solid white;
}
li.other:hover{
  cursor: pointer;
  border-bottom: 5px solid white;
}
li.other2:hover{
  cursor: pointer;
  border-bottom: 5px solid rgb(168,189,203);;
}
li>a{

  display: block;
  text-decoration: none;
  font-size: 25px;
  letter-spacing: 5px;
  color: rgba(255, 255, 255, 1);
  font-family: "Arial Black",fantasy;
  margin: 0;
}
.logo{
  vertical-align: middle;
}
.charts{
  width: 80%;
  height: 100px;
  border-radius: 25px;
  //border: 3px solid black;
  margin: 3% auto 0 auto;
  padding: 20px;
  border:none;
  position: relative;
  left: -50px;
}
.chart-img{
  width: 30%;
  height: 30%;
  margin:0 10% 2% 10%;
}
.top-img{
  width: 76vw;
  height: 11vh;
  margin-left: 12%;
}
.echart{
  width: 120%;
  height: 100%;
}
.pie-div{
  position: relative;
  width: 800px;
  height: 400px;
  right:-495px;
  top: -438px;
  border: rgb(237,237,237) 1px solid;
  background-color: rgb(237,237,237);
  border-radius: 15px;
}
.pic-div{
  width: 100%;

}
</style>